import Reflux from "reflux";
import {Message} from "@biz/Components";
import GUtils from "@/utils";
import update from "immutability-helper";
export default class BaseStore extends Reflux.Store {
    constructor(props,config,  Action) {
        super(props);
        this.listenables = Action;
        this.props = props;
        this.Action = Action;
        this.config = config;
        this.state = this.getInitialState();

    }
    getInitialState() {
        let initialSo = this.getInitialSo()
        let [path, detailStatus] =  GUtils.getUrlPath();
        this.path = path;

        return {
            detailStatus,
            initialSo,
            exportInfo: { initialSo, Action: this.Action },

            so: { ...this.config.defaultSo, ...initialSo },
            voList: [],
            pagination: {
                current: null,
                pageSize: this.config.PAGE_SIZE,
                ...this.config.pagination,
            },
            loading: false,


            detail: {},
        }
    }
    // 获取初始的查询条件
    getInitialSo() {
        return {}
    }

    // 查询开始
    onSearch(so) {
        this.setState({
            so,
            selectedRowKeys: [],
            selectedRows: [],
            loading: true,
            pagination: { ...this.state.pagination, current: so.pageNumber, pageSize: so.pageSize },
        })
    }

    doAfterSearchCompleted(){

    }
    onSearchCompleted(res) {
        if (res.success) {
            this.setState({
                voList: res.voList,
                loading: false,
                pagination: { ...this.state.pagination, total: parseInt(res.voTotal, 10) },
                exportInfo: {
                    ...this.state.exportInfo,
                    onExport: this.Action.export && this.Action.export.bind(this, this.state.so),
                    voTotal: res.voTotal || (res.voList && res.voList.length), // 有些页面是查询所有的，比如库存
                },
            })
            this.doAfterSearchCompleted();
            // 让表格处理一下高度
            GUtils.trigger(window,"resize");
        } else {
            Message.error(res.errMsg)
            this.setState({ loading: false })
        }
    }
    onSearchFailed() {
        this.setState({
            loading: false,
            voList: [],
            pagination: { ...this.state.pagination, current: 0, total: 0 },
        })
    }

    // 重置查询条件
    onResetSo() {
        let { so } = this.state
        let { pageSize } = so
        so = { ...this.config.defaultSo }
        so.pageSize = pageSize
        this.setState({ so })
    }

    // 改变每页数量
    onChangePageSize(pageSize) {
        this.setState(
            update(this.state, {
                so: { pageSize: { $set: pageSize } },
                pagination: { pageSize: { $set: pageSize } },
            })
        )
    }

    // 改变当前页码
    onChangeCurrent(current) {
        let { so } = this.state
        so = { ...so, pageNumber: current }
        this.Action.search(so)
        this.setState(
            update(this.state, {
                so: { pageNumber: { $set: current } },
                pagination: { current: { $set: current } },
            })
        )
    }
    onUnmount() {
        this.stopListeningToAll()
    }

    onRowSelectChange(selectedRowKeys, selectedRows) {
        this.setState({ selectedRowKeys, selectedRows })
    }


    // 根据URL，切换详情界面
    onHandlePageState() {
        /*let status = GUtils.getUrlParam("status")
        let id = GUtils.getUrlParam("id")
        switch (status) {
            case "create":
                this.Action.preCreate()
                break
            case "edit":
                this.Action.preEdit({ id })
                break
            case "detail":
                this.Action.preDetail({ id })
                break
            default:
                break
        }*/
    }

    onGoCreate(path){
        window.RouterGo("/page"+this.path+"/detail/create");
    }
    onGoEdit(id){
        window.RouterGo("/page"+this.path+"/detail/edit?id=$id");
    }
    onGoDetail(id){
        window.RouterGo("/page"+this.path+"/detail/view?id=$id");
    }
    onDoLoading(loading = true) {
        this.setState({ loading })
    }
    dealResVoList(res, field) {
        this.dealResponse(res, () => {
            this.setState({ [field]: res.voList || [] })
        })
    }
    dealRes(res, field) {
        this.dealResponse(res, () => {
            this.setState({ [field]: res.vo || [] })
        })
    }

    dealResponse(res, callback) {
        if (res.success) {
            callback && callback(res)
        } else {
            Message.error(res.errMsg)
        }
    }
    dealCommonResponse(res, callback) {
        this.dealResponse(res, () => {
            Message.success("操作成功")
            callback && callback(res)
        })
    }

    onCreate(){
        this.onDoLoading(true);
    }

    onCreateCompleted(res){
        this.onDoLoading(false);
        this.dealCommonResponse(res,()=>{

            window.RouterGo("/page"+this.path);
            //this.setState({detailStatus:"view"})
        })
    }

    onEdit(){
        this.onDoLoading(true);
    }

    onEditCompleted(res){
        this.onDoLoading(false);
        this.dealCommonResponse(res,()=>{
            //this.setState({detailStatus:"view"})

            window.RouterGo("/page"+this.path);
        })
    }

    onGetDetail(){
        this.onDoLoading(true);
        this.setState({loading:true, detailLoading:true, getDetailCompleted:false})
    }

    onGetDetailCompleted(callback, res){
        this.setState({loading:false, detailLoading:false});
        this.dealResponse(res,()=>{
            let {vo} = res;
            callback && callback(vo);
            this.setState({detail:vo, getDetailCompleted:true})
        })
    }
    refreshList(){
        this.Action.search(this.state.so);
    }
    onDelete(){
        this.onDoLoading(true);
    }
    onDeleteCompleted(res){
        this.onDoLoading(false);
        this.dealCommonResponse(res,()=>{
            this.refreshList();
        })
    }

    onCancel(){
        this.setState({detailLoading:false, detail:{}, detailStatus:"",modalStatus:""})
    }

    onCancelModal(){
        this.setState({modalStatus:""})
    }
    onEnabled() {
        this.onDoLoading(true);
    }
    onEnabledCompleted(res) {
        this.onDoLoading(false);
        this.dealCommonResponse(res,()=>this.refreshList())
    }
    onDelete() {
        this.onDoLoading(true);
    }
    onDeleteCompleted(res) {
        this.onDoLoading(false);
        this.dealCommonResponse(res,()=>this.refreshList())
    }
    onSort() {
        this.onDoLoading(true);
    }
    onSortCompleted(res) {
        this.onDoLoading(false);
        this.dealCommonResponse(res,()=>this.refreshList())
    }

    onSetNewState(newState){
        this.setState(newState);
    }
}